//
//  HMMainViewController.swift
//  HM22
//
//  Created by Apple on 16/11/29.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit

class HMMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //  添加子视图控制器
//        viewControllers = []
//        addChildViewController(<#T##childController: UIViewController##UIViewController#>)
        
        
        //  设置tabbar
        let hmTabbar = HMTabBar()
        //  设置代理
        hmTabbar.hmTabBarDelegate = self
        //  解决循环引用
        hmTabbar.callBack = {[weak self] in
            print(self)
        }
        
        
//        self.tabBar = hmTabbar
        //  使用kvc给只读属性赋值
        setValue(hmTabbar, forKey: "tabBar")
        
        //  添加子视图控制器
        addChildViewController(childController: HMHomeTableViewController(), imageName: "tabbar_home", title: "首页")
        addChildViewController(childController: HMMessageTableViewController(), imageName: "tabbar_message_center", title: "消息")
        addChildViewController(childController: HMDiscoverTableViewController(), imageName: "tabbar_discover", title: "发现")
        addChildViewController(childController: HMProfileTableViewController(), imageName: "tabbar_profile", title: "我的")
    }

    func addChildViewController(childController: UIViewController, imageName: String, title: String) {
        
        //  设置tabbarItem的图片
        childController.tabBarItem.image = UIImage(named: imageName)
        //  设置渲染方式
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        
        //  设置字体颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .selected)
        
        //  设置字体大小
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 12)], for: .normal)
        
        //  图片有文字可以调整图片的内间距
//        childController.tabBarItem.imageInsets
        //  设置tabarItem的文字
//        childController.tabBarItem.title = title
        //  设置导航栏的文字
//        childController.navigationItem.title = title
        
        //  合成一句代码
        childController.title = title
        
        
        //  添加到导航控制器
        let nav = HMNavigationController(rootViewController: childController)
        
        //  添加子视图控制器
        addChildViewController(nav)
        
        
    }
    
    
    

    

}


extension HMMainViewController: HMTabBarDelegate {
    func composeOperation() {
        print("代理对象")
    }
}









